//
//  YFMobManager.m
//  project
//
//  Created by Winny Zhang on 2019/3/29.
//  Copyright © 2019 健健. All rights reserved.
//

#import "YFMobManager.h"
#import "YFMobManagerConst.h"

@interface YFMobManager () <NSCopying, NSMutableCopying>

@end



@implementation YFMobManager

#pragma mark - 绝对单例
static YFMobManager *_manager = nil;
+ (instancetype)shared {
    
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        _manager = [[[self class] alloc] init];
    });
    return _manager;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        _manager = [super allocWithZone:zone];
    });
    return _manager;
}

- (instancetype)copyWithZone:(NSZone *)zone {
    return _manager;
}

- (instancetype)mutableCopyWithZone:(NSZone *)zone {
    return _manager;
}

/**
 注册当前所有平台
 */
+ (void)registerAllPlatforms {
    [[YFMobManager shared] registerWith:(YFMobPlatformWechat | YFMobPlatformQQ | YFMobPlatformSina)];
}

/**
 Class 方法 注册第三方平台
  多个平台采用位运算方式传入

 @param platform 平台类型
 */
+ (void) registerWith:(YFMobPlatform)platform {
    [[YFMobManager shared] registerWith:platform];
}

/**
 注册第三方平台

 @param platform 平台类型
 */
- (void) registerWith:(YFMobPlatform)platform {
    [ShareSDK registPlatforms:^(SSDKRegister *platformsRegister) {
        // 注册微信
        if (platform & YFMobPlatformWechat) {
            LKLog(@"注册微信");
            [platformsRegister setupWeChatWithAppId:WechatAppId appSecret:WechatAppSecret];
        }
        // 注册 QQ
        if (platform & YFMobPlatformQQ) {
            LKLog(@"注册QQ");
            [platformsRegister setupQQWithAppId:QQAppId appkey:QQAppKey];
        }
        // 注册 新浪
        if (platform & YFMobPlatformSina) {
            LKLog(@"注册新浪");
            [platformsRegister setupSinaWeiboWithAppkey:SinaAppKey appSecret:SinaAppSecret redirectUrl:SinaRedirectUrl];
        }
    }];
}

/**
 Class 方法 授权登录
 无额外设置

 @param platform 授权登录平台
 @param completion 完成回调
 */
+ (void) authorize:(YFMobPlatform)platform completion:(YFAuthorizeCompletion)completion {
    [[YFMobManager shared] authorize:platform settings:nil completion:completion];
}

/**
 Class 方法 授权登录
 有额外设置

 @param platform 授权登录平台
 @param settings 额外设置信息
 @param completion 完成回调
 */
+ (void)authorize:(YFMobPlatform)platform settings:(NSDictionary * _Nullable)settings completion:(YFAuthorizeCompletion)completion {
    [[YFMobManager shared] authorize:platform settings:settings completion:completion];
}

/**
 实例方法 授权登录

 @param platform 授权登录平台
 @param settings 额外设置信息
 @param completion 完成回调
 */
- (void)authorize:(YFMobPlatform)platform settings:(NSDictionary * _Nullable)settings completion:(YFAuthorizeCompletion)completion {
     NSDictionary *authorSettings = settings.count > 0 ? settings : nil;
    
    // 微信授权登录
    if (platform & YFMobPlatformWechat) {
        LKLog(@"微信授权登录");
        [ShareSDK authorize:SSDKPlatformTypeWechat settings:authorSettings onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                completion(YES, user, [self authorizeErrorDescriptionWith:state]);
            } else {
                completion(NO, user, [self authorizeErrorDescriptionWith:state]);
            }
        }];
    }
    
    // QQ授权登录
    if (platform & YFMobPlatformQQ ) {
        LKLog(@"QQ授权登录");
        if (![ShareSDK isClientInstalled:(SSDKPlatformTypeQQ)]) {
            completion(NO, nil, @"您的手机未安装QQ");
            return;
        }
        [ShareSDK authorize:(SSDKPlatformTypeQQ) settings:authorSettings onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                completion(YES, user, [self authorizeErrorDescriptionWith:state]);
            } else {
                completion(NO, user, [self authorizeErrorDescriptionWith:state]);
            }
        }];
    }
    
    // 新浪授权登录
    if (platform & YFMobPlatformSina) {
        LKLog(@"新浪授权登录");
        [ShareSDK authorize:(SSDKPlatformTypeSinaWeibo) settings:authorSettings onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) {
            if (state == SSDKResponseStateSuccess) {
                completion(YES, user, [self authorizeErrorDescriptionWith:state]);
            } else {
                completion(NO, user, [self authorizeErrorDescriptionWith:state]);
            }
        }];
    }
}

/**
 包装返回信息

 @param state 状态
 @return 对应的message字符串
 */
- (NSString *)authorizeErrorDescriptionWith:(SSDKResponseState)state {
    switch (state) {
        case SSDKResponseStateBegin:
            return @"正在开始授权";
            break;
        case SSDKResponseStateSuccess:
            return @"授权成功！";
            break;
        case SSDKResponseStateFail:
            return @"授权失败";
            break;
        case SSDKResponseStateCancel:
            return @"取消授权";
            break;
        case SSDKResponseStateUpload:
            return @"";
            break;
        default:
            return @"";
            break;
    }
}

//分享到其他客户端
- (void)shareWithPlatformType:(SSDKPlatformType)platformType url:(NSString *)url title:(NSString*)title descripte:(NSString *)descripte {
    if (!url.length) {
        LKLog(@"分享URL地址为空");
        return;
    }
    if (!title.length) {
        title = @"逗比女神，有颜有趣，各种段子海量刷新。";
    }
    if (!descripte.length) {
        descripte = @"没时间解释了，快上车！";
    }
    
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:descripte
                                     images:@[[UIImage imageNamed:@"logo_lejian"]] //传入要分享的图片
                                        url:[NSURL URLWithString:url]
                                      title:title
                                       type:SSDKContentTypeAuto];
    
    [ShareSDK share:platformType parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
        switch (state) {
            case SSDKResponseStateUpload:
                // 分享视频的时候上传回调，进度信息在 userData
                break;
            case SSDKResponseStateSuccess:
                //成功
                break;
            case SSDKResponseStateFail:
            {
                LKLog(@"--%@",error.description);
                //失败
                break;
            }
            case SSDKResponseStateCancel:
                //取消
                break;
                
            default:
                break;
        }
    }];
}

@end
